class Solution {
    /**
     * [7, 0]
     * [7, 1]
     * [6, 1]
     * [5, 0]
     * [5, 2]
     * [4, 4]
     * 先按照(h,k)的顺序排序, 遍历people,不断插入即可
     */
    public int[][] reconstructQueue(int[][] people) {
        // 按照(h,k)的顺序排序
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                if(a[0] != b[0])
                    return b[0]-a[0];
                else
                    return a[1]-b[1];
            }
        });

        // 插入
        ArrayList<int[]> ans = new ArrayList<>();
        for (int i = 0; i < people.length; i++) {
            ans.add(people[i][1],people[i]);
        }

        // build ans
        for (int i = 0; i < ans.size(); i++) {
            people[i] = ans.get(i);
        }
        return people;
    }
}